﻿<!DOCTYPE html>

<html lang="zh-cn">
<head>
    <meta name="viewport" content="width=device-width" />
    <title>添加配置</title>
    <link href="../lib/layui/css/layui.min.css" rel="stylesheet" />
    <link href="../x-admin/css/font.css" rel="stylesheet" />
    <link href="../x-admin/css/xadmin.css" rel="stylesheet" />
    <link href="../lib/layui/lay/plugins/eleTree/font/eleTree.css" rel="stylesheet" />
    <script src="../lib/cookie/cookie.js"></script>
    <style>
        .ele {
            height: auto;
            width: 100%;
            display: none;
            position: absolute;
            top: 100%;
            background-color: #fff;
            z-index: 100;
        }

        .eleTree {
            height: 260px;
            border: 1px solid #eee;
            overflow: scroll;
            display: inline-block;
        }
        #typediv .layui-form-select dl
        {
            max-height:260px;
        }
    </style>
    <script type="text/javascript" src="../lib/Base64/base64.min.js"></script>
</head>
<body>
    <div class="layui-card-body" style="">
        <form class="layui-form" action="" name="newItem" id="addItem">
            <input type="hidden" name="moduleCfgName" id="moduleCfgName" lay-verify="title" autocomplete="off" value="" class="layui-input">
            <input type="text" name="oldNumber" id="oldNumber" lay-verify="title" autocomplete="off" value="" class="layui-input" style="display:none">

            <div class="layui-form-item">
                <label class="layui-form-label">步骤序号:</label>
                <div class="layui-input-block">
                    <input type="text" name="number" id="number" lay-verify="title" autocomplete="off" required value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">执行类型:</label>
                <div class="layui-input-block">
                    <select name="executeType" id="executeType" lay-filter="executeType" lay-verify="required">
                        <option value="xml">xml文件</option>
                        <option value="xml_var">xml自定义</option>
                        <option value="dll">dll包</option>
                        <option value="reg" selected>正则</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">步骤编号:</label>
                <div class="layui-input-block" id="typediv">
                    <input type="text" name="code" id="code" lay-verify="title" autocomplete="off" required value="" class="layui-input">
                    <!--<select name="code" id="code"  lay-verify="required" lay-search="">
                        <option value="">直接选择或搜索选择</option>
                    </select>-->
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" id="labname">配置文件</label>
                <div class="layui-input-block">
                    <input type="text" id="executeFile" name="executeFile" autocomplete="off" class="layui-input" />
                    <div class="eleTree ele" id="selectFile" lay-filter="selectFile"></div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">中文名称:</label>
                <div class="layui-input-block">
                    <input type="text" name="zh_cn" lay-verify="title" autocomplete="off" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formmoudleitem">立即提交</button>
                </div>
            </div>
        </form>
    </div>
    <script src="../lib/layui/layui.js"></script>
    <script type="text/javascript">

        function getrequest() {
            var url = location.search; //获取url中"?"符后的字串
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        }
        var pars = getrequest();
        var fileName = JSON.parse(window.decodeURIComponent(atob(pars.fileName)));
        var $ = layui.$;
        layui.$().ready(function () {
            var $ = layui.$;
            var form = layui.form;
            $("#selectFile").hide();
            var itemtemp = getrequest();
            if (itemtemp != "") {
                var fileName = JSON.parse(window.decodeURIComponent(atob(itemtemp.fileName)));
                var stepNo = JSON.parse(window.decodeURIComponent(atob(itemtemp.stepNo)));
                $("input[name='moduleCfgName']").val(fileName);
                $("input[name='number']").val(stepNo);
            }
            var executeType = $("#executeType").val();

            switch (executeType) {
                case 'reg':
                    $("#labname").text("正则编号");
                    $("#executeFile").removeAttr("readonly");
                    break;
                case 'xml':
                case 'dll':
                    $("#labname").text("配置文件");
                    $("#executeFile").attr("readonly", "readonly");
                    break;
            }
        });

        function getcodeinfos(type) {
            if (type == "input") {
                //步骤编号是输入框
                $("#typediv").html("<input type=\"text\" name=\"code\" id=\"code\" lay-verify=\"title\" autocomplete=\"off\" required value=\"\" class=\"layui-input\">");
            } else {
                //步骤编号是下拉框
                //获取配置文件信息
                $.ajax({
                    url: "../xmlstepcode.json",
                    async: false,
                    type: "GET",
                    headers: {
                        user: getCookie("userinfo"),
                        version: getCookie("version")
                    },
                    dataType: "json",
                    success: function (jsonm) {
                        // 写入数据
                        var output = [];
                        $.each(jsonm, function (key, value) {
                            output.push('<option value="' + key + '">' + value + '</option>');
                        });
                        $("#typediv").html("<select name='code' id='code'  lay-verify='required' lay-search=''>" + output.join('') + "</select >");
                    }
                });
            }
        }

        layui.config({
            base: '/lib/layui/lay/plugins/'
        }).extend({
            treeSelect: 'treeSelect/treeSelect',
            eleTree: 'eleTree/eleTree'
        }).use(['form', 'treeSelect', 'eleTree'], function () {
            var form = layui.form;
            var treeSelect = layui.treeSelect;
            var eleTree = layui.eleTree;
            var $ = layui.$;
            //监听提交
            form.on('submit(formmoudleitem)', function (data) {
                $.ajax({
                    type: "post",
                    url: localStorage.getItem('hosttingaddr') + '/api/moudlecfgfile/uploadfileofitem',
                    data: $('#addItem').serialize(),
                    headers: {
                        user: getCookie("userinfo"),
                        version: getCookie("version")
                    },
                    success: function (data) {
                        if (data == "success") {
                            layer.msg('添加完成!', { icon: 1,time:1000 }, function () {
                                parent.layui.table.reload('moudlestepinfo');
                                var index = parent.layer.getFrameIndex(window.name); //获取当前窗口的name
                                parent.layer.close(index);
                            });
                        }
                    },
                    error: function (data) {
                        layer.msg(data.responseText, {icon:5}); 
                    }
                });
                return false;
            });

            //执行类型下拉框change事件
            form.on('select(executeType)', function (obj) {
                var executeType = obj.value;
                // 当filetype时xml或是dll时下拉出对应类型的文件，否则设置成可输的编辑框(此时主要对应的正则时候输入正则编号)
                switch (executeType) {
                    case 'reg':
                        $("#executeFile").removeAttr("readonly");
                        $("#selectFile").hide();
                        $("#labname").text("正则编号");
                        $("#executeFile").attr("placeholder", "请输入正则编号");
                        getcodeinfos("input");
                        break;
                    case 'xml_var':
                        $("#executeFile").removeAttr("readonly");
                        $("#selectFile").hide();
                        $("#executeFile").attr("placeholder","文件格式：a/b/c/{casetype}_{causecode}.html");
                        $("#labname").text("自定义文件");
                        getcodeinfos();
                        break;
                    case 'xml':
                    case 'dll':
                        $("#executeFile").removeAttr("placeholder");
                        $("#executeFile").attr("readonly", "readonly");
                        $("#labname").text("配置文件");
                        $("#selectFile").hide();
                        getcodeinfos();
                        break;
                }
                layui.form.render('select');
            });

            //配置文件文本框click事件
            $("#executeFile").on("click", function (e) {
                e.stopPropagation();
                var executeType = $("#executeType").val();
                //|| executeType=="xml_var"
                if (executeType == "xml" || executeType == "dll") {
                    $("#selectFile").toggle();
                }
                
            })
            var el= eleTree({
                el: '#selectFile',
                method: 'get',
                url: localStorage.getItem('hosttingaddr') + '/api/Commonconfig/GetCommonConfigTree',
                headers: {
                    user: getCookie("userinfo"),
                    version: getCookie("version")
                },
                defaultExpandAll: false,
                expandOnClickNode: false,
                highlightCurrent: true,
                showLine: true,
                response: {
                    statusName: 'code',
                    statusCode: 0,
                    dataName: 'data'
                },
                request: {
                    name: 'name',
                    key: 'fullName',
                    children: 'children',
                    checked: 'checked',
                    disabled: 'disabled',
                    isLeaf: 'isLeaf'
                },
                done: function (res) {
                    // res即为你接口返回的信息。
                    console.log(res);
                }
            });
            el.on("click", function (d) {
                if (d.data.noteType == "F") {
                    $("#executeFile").val(d.data.fullName);
                    $("#selectFile").hide();
                }
                
            }) 
        });
        
    </script>
</body>
</html>